﻿using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Avalonia.Controls.Notifications;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YMCL.Public.Classes;
using YMCL.Public.Classes.Netease;
using YMCL.Public.Classes.Operate;
using YMCL.Public.Classes.Setting;
using YMCL.Public.Controls;
using YMCL.Public.Enum;
using YMCL.Public.Langs;
using YMCL.Public.Module;
using YMCL.Public.Module.IO.Network;
using YMCL.Public.Module.Ui;
using YMCL.ViewModels;

namespace YMCL.Views.Main.Pages;

public partial class Music : UserControl
{
    public readonly MusicPages.Lyric Lyric = new();
    public readonly MusicPages.PlayList Playlist = new();
    public readonly MusicPages.Search Search = new();
    private bool _isPlaying;
    private bool _isLyric;
    private readonly Debouncer _debouncer;
    private RecordSongEntry? _lastSongEntry;

    public Music()
    {
        _debouncer =
            new Debouncer(
                () =>
                {
                    Dispatcher.UIThread.Invoke(() =>
                    {
                        AudioPlayer.Instance.UpdateProgress(ControlPlayerSlider.Value);
                    });
                }, 10);
        InitializeComponent();
        RightControl.Content = Playlist;
        LeftControl.Content = Search;
        DataContext = Data.Instance;
        BindingEvent();
    }

    private void BindingEvent()
    {
        Data.UiProperty.PropertyChanged += async (o, e) =>
        {
            var song = Data.UiProperty.SelectedRecordSong;
            if (e.PropertyName != nameof(UiProperty.SelectedRecordSong) ||
                song == null) return;
            if (_lastSongEntry == song) return;
            _lastSongEntry = song;
            AudioPlayer.Instance.Stop();
            Data.UiProperty.MusicCurrentTime = 0;
            Data.UiProperty.MusicTotalTime = 0;
            ToggleLoadingUi(true);
            PlayUi();
            _ = Lyric.LoadLyric(song);
            if (song.Type == SongType.Local)
            {
                Data.UiProperty.MusicTotalTime = AudioPlayer.Instance.PlayLocal(song.Path);
                ToggleLoadingUi(false);
            }
            else if (song.Type == SongType.Netease)
            {
                var ava = await NeteaseMusic.GetMusicAvailabilityById(song.SongId);
                if (!ava)
                {
                    Notice(MainLang.MusicNotAvailable, NotificationType.Error);
                    Data.UiProperty.SelectedRecordSong = null;
                    ToggleLoadingUi(false);
                    return;
                }

                var url = await NeteaseMusic.GetSongUrlByIdAndLevel(song.SongId);
                if (string.IsNullOrWhiteSpace(url.url))
                {
                    Notice(MainLang.NetWorkError, NotificationType.Error);
                    Data.UiProperty.SelectedRecordSong = null;
                    ToggleLoadingUi(false);
                    return;
                }
                Data.UiProperty.MusicTotalTime = url.ms;
                await AudioPlayer.Instance.PlayNetwork(url.url);
                ToggleLoadingUi(false);
            }
        };
        PlayBtn.PointerPressed += (_, e) =>
        {
            if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
            if (_isPlaying)
                PauseUi();
            else
                PlayUi();
        };
        LyricBtn.PointerPressed += (o, e) =>
        {
            if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
            if (_isLyric)
            {
                LeftControl.Content = Search;
                _isLyric = false;
            }
            else
            {
                LeftControl.Content = Lyric;
                _isLyric = true;
            }

            _ = Animator.PageLoading.LevelTwoPage(LeftControl.Content as UserControl);
        };
        NextSongBtn.PointerPressed += (_, _) => Playlist.NextSong();
        PreviousSongBtn.PointerPressed += (_, _) => Playlist.PreviousSong();
        AudioPlayer.Instance.ProgressChanged += (_, e) => { Data.UiProperty.MusicCurrentTime = e.CurrentTime; };
        ControlPlayerSlider.ValueChanged += (_, _) =>
        {
            _debouncer.Trigger();
            Data.UiProperty.MusicCurrentTime = ControlPlayerSlider.Value;
        };
        AudioPlayer.Instance.PlaybackCompleted += (_, _) =>
        {
            if (Math.Abs(Data.UiProperty.MusicCurrentTime - Data.UiProperty.MusicTotalTime) > 1000 ||
                Data.UiProperty.MusicTotalTime == 0) return;
            switch (Data.SettingEntry.Repeat)
            {
                case Public.Enum.Setting.Repeat.RepeatAll:
                    if (Data.RecordSongEntries.Count > 1)
                        Playlist.NextSong();
                    else
                        AudioPlayer.Instance.UpdateProgress(0);
                    // await System.Threading.Tasks.Task.Delay(1000);
                    AudioPlayer.Instance.Resume();
                    break;
                case Public.Enum.Setting.Repeat.RepeatOne:
                    AudioPlayer.Instance.UpdateProgress(0);
                    // await System.Threading.Tasks.Task.Delay(1000);
                    AudioPlayer.Instance.Resume();
                    break;
                default:
                    PauseUi();
                    break;
            }
        };
        VolumeBtn.PointerPressed += (_, _) =>
        {
            if (VolumeRoot.Opacity == 0)
                VolumeRoot.Opacity = (double)Application.Current.Resources["MainOpacity"]!;
            else
                VolumeRoot.Opacity = 0;
        };
        RepeatBtn.PointerPressed += (_, _) =>
        {
            switch (Data.SettingEntry.Repeat)
            {
                case Public.Enum.Setting.Repeat.RepeatOff:
                    Data.SettingEntry.Repeat = Public.Enum.Setting.Repeat.RepeatAll;
                    RepeatBtn.Data = PathGeometry.Parse(
                        "F1 M 7.5 3.75 C 6.634114 3.75 5.82194 3.912762 5.063477 4.238281 C 4.305013 4.563803 3.642578 5.009766 3.076172 5.576172 C 2.509766 6.142578 2.063802 6.805014 1.738281 7.563477 C 1.41276 8.32194 1.25 9.134115 1.25 10 C 1.25 10.527344 1.318359 11.056315 1.455078 11.586914 C 1.591797 12.117514 1.79362 12.613933 2.060547 13.076172 C 2.099609 13.147787 2.150065 13.225912 2.211914 13.310547 C 2.273763 13.395183 2.333984 13.483073 2.392578 13.574219 C 2.451172 13.665365 2.501627 13.756511 2.543945 13.847656 C 2.586263 13.938803 2.607422 14.023438 2.607422 14.101562 C 2.607422 14.270834 2.545573 14.417318 2.421875 14.541016 C 2.298177 14.664714 2.151693 14.726562 1.982422 14.726562 C 1.871745 14.726562 1.778971 14.703776 1.704102 14.658203 C 1.629232 14.612631 1.55599 14.550781 1.484375 14.472656 C 1.24349 14.192709 1.030273 13.870443 0.844727 13.505859 C 0.65918 13.141276 0.504557 12.760417 0.380859 12.363281 C 0.257161 11.966146 0.16276 11.564128 0.097656 11.157227 C 0.032552 10.750326 0 10.364584 0 10 C 0 9.316406 0.089518 8.6556 0.268555 8.017578 C 0.447591 7.379558 0.69987 6.782227 1.025391 6.225586 C 1.350911 5.668945 1.741536 5.162761 2.197266 4.707031 C 2.652995 4.251303 3.157552 3.859051 3.710938 3.530273 C 4.264323 3.201498 4.861653 2.947592 5.50293 2.768555 C 6.144206 2.58952 6.809896 2.5 7.5 2.5 L 14.121094 2.5 L 12.685547 1.064453 C 12.561849 0.940756 12.5 0.794271 12.5 0.625 C 12.5 0.45573 12.561849 0.309246 12.685547 0.185547 C 12.809244 0.06185 12.955729 0 13.125 0 C 13.294271 0 13.440755 0.06185 13.564453 0.185547 L 16.064453 2.685547 C 16.18815 2.809246 16.25 2.95573 16.25 3.125 C 16.25 3.294271 16.18815 3.440756 16.064453 3.564453 L 13.564453 6.064453 C 13.440755 6.188151 13.294271 6.25 13.125 6.25 C 12.955729 6.25 12.809244 6.188151 12.685547 6.064453 C 12.561849 5.940756 12.5 5.794271 12.5 5.625 C 12.5 5.455729 12.561849 5.309245 12.685547 5.185547 L 14.121094 3.75 Z M 20 10 C 20 10.690104 19.91048 11.352539 19.731445 11.987305 C 19.552408 12.62207 19.300129 13.217773 18.974609 13.774414 C 18.649088 14.331055 18.258463 14.83724 17.802734 15.292969 C 17.347004 15.748698 16.842447 16.140951 16.289062 16.469727 C 15.735677 16.798502 15.138346 17.052408 14.49707 17.231445 C 13.855793 17.410482 13.190104 17.5 12.5 17.5 L 5.888672 17.5 L 7.314453 18.935547 C 7.43815 19.059244 7.5 19.205729 7.5 19.375 C 7.5 19.544271 7.43815 19.690756 7.314453 19.814453 C 7.190755 19.93815 7.044271 20 6.875 20 C 6.705729 20 6.559244 19.93815 6.435547 19.814453 L 3.935547 17.314453 C 3.811849 17.190756 3.75 17.044271 3.75 16.875 C 3.75 16.705729 3.811849 16.559244 3.935547 16.435547 L 6.435547 13.935547 C 6.559244 13.81185 6.705729 13.75 6.875 13.75 C 7.044271 13.75 7.190755 13.81185 7.314453 13.935547 C 7.43815 14.059245 7.5 14.205729 7.5 14.375 C 7.5 14.544271 7.43815 14.690756 7.314453 14.814453 L 5.888672 16.25 L 12.5 16.25 C 13.365885 16.25 14.178059 16.08724 14.936523 15.761719 C 15.694986 15.436198 16.357422 14.990234 16.923828 14.423828 C 17.490234 13.857422 17.936197 13.194987 18.261719 12.436523 C 18.587238 11.678061 18.75 10.865886 18.75 10 C 18.75 9.628906 18.723957 9.283854 18.671875 8.964844 C 18.619791 8.645834 18.541666 8.336589 18.4375 8.037109 C 18.333332 7.737631 18.204752 7.444662 18.051758 7.158203 C 17.898762 6.871746 17.721354 6.575521 17.519531 6.269531 C 17.473957 6.197917 17.441406 6.139323 17.421875 6.09375 C 17.402344 6.048178 17.392578 5.983074 17.392578 5.898438 C 17.392578 5.729168 17.452799 5.581056 17.573242 5.454102 C 17.693684 5.327149 17.841797 5.263673 18.017578 5.263672 C 18.128254 5.263673 18.221027 5.286459 18.295898 5.332031 C 18.370768 5.377604 18.44401 5.439453 18.515625 5.517578 C 18.75651 5.797527 18.969727 6.119793 19.155273 6.484375 C 19.34082 6.848959 19.495441 7.231445 19.619141 7.631836 C 19.742838 8.032227 19.837238 8.435873 19.902344 8.842773 C 19.967447 9.249675 20 9.635417 20 10 Z ");
                    break;
                case Public.Enum.Setting.Repeat.RepeatAll:
                    Data.SettingEntry.Repeat = Public.Enum.Setting.Repeat.RepeatOne;
                    RepeatBtn.Data = RepeatBtn.Data = PathGeometry.Parse(
                        "F1 M 7.5 3.75 C 6.634114 3.75 5.82194 3.912762 5.063477 4.238281 C 4.305013 4.563803 3.642578 5.009766 3.076172 5.576172 C 2.509766 6.142578 2.063802 6.805014 1.738281 7.563477 C 1.41276 8.32194 1.25 9.134115 1.25 10 C 1.25 10.384115 1.277669 10.73405 1.333008 11.049805 C 1.388346 11.365561 1.468099 11.669922 1.572266 11.962891 C 1.676432 12.255859 1.806641 12.543945 1.962891 12.827148 C 2.119141 13.110352 2.294922 13.411459 2.490234 13.730469 C 2.529297 13.795573 2.560221 13.855795 2.583008 13.911133 C 2.605794 13.966472 2.617188 14.029948 2.617188 14.101562 C 2.617188 14.270834 2.553711 14.417318 2.426758 14.541016 C 2.299805 14.664714 2.151693 14.726562 1.982422 14.726562 C 1.871745 14.726562 1.778971 14.703776 1.704102 14.658203 C 1.629232 14.612631 1.55599 14.550781 1.484375 14.472656 C 1.25 14.199219 1.040039 13.880209 0.854492 13.515625 C 0.668945 13.151042 0.512695 12.766928 0.385742 12.363281 C 0.258789 11.959636 0.16276 11.552734 0.097656 11.142578 C 0.032552 10.732422 0 10.351562 0 10 C 0 8.990886 0.188802 8.032227 0.566406 7.124023 C 0.94401 6.215821 1.48763 5.406902 2.197266 4.697266 C 2.906901 3.987631 3.71582 3.444012 4.624023 3.066406 C 5.532227 2.688803 6.490885 2.5 7.5 2.5 L 14.121094 2.5 L 12.685547 1.064453 C 12.561849 0.940756 12.5 0.794271 12.5 0.625 C 12.5 0.45573 12.561849 0.309246 12.685547 0.185547 C 12.809244 0.06185 12.955729 0 13.125 0 C 13.294271 0 13.440755 0.06185 13.564453 0.185547 L 16.064453 2.685547 C 16.18815 2.809246 16.25 2.95573 16.25 3.125 C 16.25 3.294271 16.18815 3.440756 16.064453 3.564453 L 13.564453 6.064453 C 13.440755 6.188151 13.294271 6.25 13.125 6.25 C 12.955729 6.25 12.809244 6.188151 12.685547 6.064453 C 12.561849 5.940756 12.5 5.794271 12.5 5.625 C 12.5 5.455729 12.561849 5.309245 12.685547 5.185547 L 14.121094 3.75 Z M 17.392578 5.898438 C 17.392578 5.729168 17.452799 5.581056 17.573242 5.454102 C 17.693684 5.327149 17.841797 5.263673 18.017578 5.263672 C 18.128254 5.263673 18.221027 5.286459 18.295898 5.332031 C 18.370768 5.377604 18.44401 5.439453 18.515625 5.517578 C 18.75 5.784506 18.958332 6.097006 19.140625 6.455078 C 19.322916 6.813152 19.47591 7.189129 19.599609 7.583008 C 19.723307 7.976889 19.819336 8.374023 19.887695 8.774414 C 19.956055 9.174805 19.993488 9.550781 20 9.902344 C 19.791666 9.648438 19.570312 9.405925 19.335938 9.174805 C 19.101562 8.943686 18.85091 8.733725 18.583984 8.544922 C 18.479816 8.115235 18.334961 7.721355 18.149414 7.363281 C 17.963867 7.005209 17.753906 6.640625 17.519531 6.269531 C 17.473957 6.197917 17.441406 6.139323 17.421875 6.09375 C 17.402344 6.048178 17.392578 5.983074 17.392578 5.898438 Z M 20 14.375 C 20 15.14974 19.851887 15.878906 19.555664 16.5625 C 19.259439 17.246094 18.857422 17.841797 18.349609 18.349609 C 17.841797 18.857422 17.246094 19.259439 16.5625 19.555664 C 15.878906 19.851889 15.149739 20 14.375 20 C 13.59375 20 12.861328 19.853516 12.177734 19.560547 C 11.494141 19.267578 10.898438 18.867188 10.390625 18.359375 C 9.882812 17.851562 9.482422 17.255859 9.189453 16.572266 C 8.896484 15.888672 8.75 15.15625 8.75 14.375 C 8.75 13.600261 8.898111 12.871094 9.194336 12.1875 C 9.49056 11.503906 9.892578 10.908203 10.400391 10.400391 C 10.908203 9.892578 11.503906 9.490561 12.1875 9.194336 C 12.871093 8.898112 13.60026 8.75 14.375 8.75 C 14.889322 8.75 15.385741 8.816732 15.864258 8.950195 C 16.342773 9.083659 16.790363 9.272461 17.207031 9.516602 C 17.623697 9.760742 18.004557 10.055339 18.349609 10.400391 C 18.69466 10.745443 18.989258 11.126303 19.233398 11.542969 C 19.477539 11.959636 19.66634 12.407227 19.799805 12.885742 C 19.933268 13.364258 20 13.860678 20 14.375 Z M 15 11.875 C 14.999999 11.705729 14.93815 11.559245 14.814453 11.435547 C 14.690755 11.31185 14.544271 11.25 14.375 11.25 C 14.205729 11.25 14.059244 11.31185 13.935547 11.435547 L 12.685547 12.685547 C 12.561849 12.809245 12.5 12.955729 12.5 13.125 C 12.5 13.294271 12.561849 13.440756 12.685547 13.564453 C 12.809244 13.688151 12.955729 13.75 13.125 13.75 C 13.268229 13.75 13.385416 13.712565 13.476562 13.637695 C 13.567708 13.562826 13.658854 13.479818 13.75 13.388672 L 13.75 16.875 C 13.75 17.044271 13.811849 17.190756 13.935547 17.314453 C 14.059244 17.43815 14.205729 17.5 14.375 17.5 C 14.544271 17.5 14.690755 17.43815 14.814453 17.314453 C 14.93815 17.190756 14.999999 17.044271 15 16.875 Z M 7.431641 16.25 C 7.548828 16.686197 7.705078 17.102865 7.900391 17.5 L 5.888672 17.5 L 7.314453 18.935547 C 7.43815 19.059244 7.5 19.205729 7.5 19.375 C 7.5 19.544271 7.43815 19.690756 7.314453 19.814453 C 7.190755 19.93815 7.044271 20 6.875 20 C 6.705729 20 6.559244 19.93815 6.435547 19.814453 L 3.935547 17.314453 C 3.811849 17.190756 3.75 17.044271 3.75 16.875 C 3.75 16.705729 3.811849 16.559244 3.935547 16.435547 L 6.435547 13.935547 C 6.559244 13.81185 6.705729 13.75 6.875 13.75 C 7.005208 13.75 7.115885 13.782553 7.207031 13.847656 C 7.200521 13.938803 7.195638 14.02832 7.192383 14.116211 C 7.189127 14.204102 7.1875 14.29362 7.1875 14.384766 C 7.1875 14.475912 7.189127 14.56543 7.192383 14.65332 C 7.195638 14.741211 7.200521 14.830729 7.207031 14.921875 L 5.888672 16.25 Z ");
                    break;
                default:
                    Data.SettingEntry.Repeat = Public.Enum.Setting.Repeat.RepeatOff;
                    RepeatBtn.Data = RepeatBtn.Data = PathGeometry.Parse(
                        "F1 M 20 19.375 C 20 19.544271 19.93815 19.690756 19.814453 19.814453 C 19.690754 19.93815 19.54427 20 19.375 20 C 19.205729 20 19.059244 19.93815 18.935547 19.814453 L 0.185547 1.064453 C 0.061849 0.940756 0 0.794271 0 0.625 C 0 0.45573 0.061849 0.309246 0.185547 0.185547 C 0.309245 0.06185 0.455729 0 0.625 0 C 0.794271 0 0.940755 0.06185 1.064453 0.185547 L 19.814453 18.935547 C 19.93815 19.059244 20 19.205729 20 19.375 Z M 7.5 3.75 C 7.330729 3.75 7.161458 3.756512 6.992188 3.769531 C 6.822916 3.782553 6.653646 3.802084 6.484375 3.828125 L 5.439453 2.792969 C 6.103516 2.597656 6.790364 2.5 7.5 2.5 L 14.121094 2.5 L 12.685547 1.064453 C 12.561849 0.940756 12.5 0.794271 12.5 0.625 C 12.5 0.45573 12.561849 0.309246 12.685547 0.185547 C 12.809244 0.06185 12.955729 0 13.125 0 C 13.294271 0 13.440755 0.06185 13.564453 0.185547 L 16.064453 2.685547 C 16.18815 2.809246 16.25 2.95573 16.25 3.125 C 16.25 3.294271 16.18815 3.440756 16.064453 3.564453 L 13.564453 6.064453 C 13.440755 6.188151 13.294271 6.25 13.125 6.25 C 12.955729 6.25 12.809244 6.188151 12.685547 6.064453 C 12.561849 5.940756 12.5 5.794271 12.5 5.625 C 12.5 5.455729 12.561849 5.309245 12.685547 5.185547 L 14.121094 3.75 Z M 3.066406 3.955078 L 3.964844 4.84375 C 3.535156 5.130209 3.152669 5.463867 2.817383 5.844727 C 2.482096 6.225586 2.198893 6.63737 1.967773 7.080078 C 1.736654 7.522787 1.559245 7.991537 1.435547 8.486328 C 1.311849 8.98112 1.25 9.485678 1.25 10 C 1.25 10.709636 1.355794 11.363933 1.567383 11.962891 C 1.778971 12.56185 2.083333 13.151042 2.480469 13.730469 C 2.526042 13.795573 2.558594 13.852539 2.578125 13.901367 C 2.597656 13.950195 2.607422 14.016928 2.607422 14.101562 C 2.607422 14.270834 2.545573 14.418945 2.421875 14.545898 C 2.298177 14.672852 2.151693 14.736328 1.982422 14.736328 C 1.871745 14.736328 1.778971 14.713542 1.704102 14.667969 C 1.629232 14.622396 1.55599 14.560547 1.484375 14.482422 C 1.39974 14.391276 1.323242 14.287109 1.254883 14.169922 C 1.186523 14.052734 1.119792 13.942058 1.054688 13.837891 C 0.703125 13.251953 0.439453 12.635092 0.263672 11.987305 C 0.087891 11.339519 0 10.677084 0 10 C 0 8.769531 0.268555 7.638347 0.805664 6.606445 C 1.342773 5.574545 2.096354 4.690756 3.066406 3.955078 Z M 16.992188 14.345703 C 17.552082 13.759766 17.985025 13.092448 18.291016 12.34375 C 18.597004 11.595053 18.75 10.813803 18.75 10 C 18.75 9.290365 18.644205 8.636068 18.432617 8.037109 C 18.221027 7.438152 17.916666 6.848959 17.519531 6.269531 C 17.473957 6.204428 17.441406 6.147462 17.421875 6.098633 C 17.402344 6.049806 17.392578 5.983074 17.392578 5.898438 C 17.392578 5.729168 17.454426 5.581056 17.578125 5.454102 C 17.701822 5.327149 17.848307 5.263673 18.017578 5.263672 C 18.128254 5.263673 18.221027 5.286459 18.295898 5.332031 C 18.370768 5.377604 18.44401 5.439453 18.515625 5.517578 C 18.587238 5.602215 18.655598 5.694988 18.720703 5.795898 C 18.785807 5.896811 18.847656 5.996094 18.90625 6.09375 C 19.264322 6.686198 19.536133 7.312826 19.72168 7.973633 C 19.907227 8.63444 20 9.309896 20 10 C 20 10.976562 19.817707 11.91569 19.453125 12.817383 C 19.088541 13.719076 18.564453 14.521484 17.880859 15.224609 Z M 5.888672 16.25 L 12.5 16.25 C 13.320312 16.25 14.117838 16.090496 14.892578 15.771484 L 15.830078 16.71875 C 15.309244 16.972656 14.770508 17.166342 14.213867 17.299805 C 13.657227 17.433268 13.085938 17.5 12.5 17.5 L 5.888672 17.5 L 7.314453 18.935547 C 7.43815 19.059244 7.5 19.205729 7.5 19.375 C 7.5 19.544271 7.43815 19.690756 7.314453 19.814453 C 7.190755 19.93815 7.044271 20 6.875 20 C 6.705729 20 6.559244 19.93815 6.435547 19.814453 L 3.935547 17.314453 C 3.811849 17.190756 3.75 17.044271 3.75 16.875 C 3.75 16.705729 3.811849 16.559244 3.935547 16.435547 L 6.435547 13.935547 C 6.559244 13.81185 6.705729 13.75 6.875 13.75 C 7.044271 13.75 7.190755 13.81185 7.314453 13.935547 C 7.43815 14.059245 7.5 14.205729 7.5 14.375 C 7.5 14.544271 7.43815 14.690756 7.314453 14.814453 Z ");
                    break;
            }
        };
        Loaded += (_, _) =>
        {
            RepeatBtn.Data = Data.SettingEntry.Repeat switch
            {
                Public.Enum.Setting.Repeat.RepeatAll => PathGeometry.Parse(
                    "F1 M 7.5 3.75 C 6.634114 3.75 5.82194 3.912762 5.063477 4.238281 C 4.305013 4.563803 3.642578 5.009766 3.076172 5.576172 C 2.509766 6.142578 2.063802 6.805014 1.738281 7.563477 C 1.41276 8.32194 1.25 9.134115 1.25 10 C 1.25 10.527344 1.318359 11.056315 1.455078 11.586914 C 1.591797 12.117514 1.79362 12.613933 2.060547 13.076172 C 2.099609 13.147787 2.150065 13.225912 2.211914 13.310547 C 2.273763 13.395183 2.333984 13.483073 2.392578 13.574219 C 2.451172 13.665365 2.501627 13.756511 2.543945 13.847656 C 2.586263 13.938803 2.607422 14.023438 2.607422 14.101562 C 2.607422 14.270834 2.545573 14.417318 2.421875 14.541016 C 2.298177 14.664714 2.151693 14.726562 1.982422 14.726562 C 1.871745 14.726562 1.778971 14.703776 1.704102 14.658203 C 1.629232 14.612631 1.55599 14.550781 1.484375 14.472656 C 1.24349 14.192709 1.030273 13.870443 0.844727 13.505859 C 0.65918 13.141276 0.504557 12.760417 0.380859 12.363281 C 0.257161 11.966146 0.16276 11.564128 0.097656 11.157227 C 0.032552 10.750326 0 10.364584 0 10 C 0 9.316406 0.089518 8.6556 0.268555 8.017578 C 0.447591 7.379558 0.69987 6.782227 1.025391 6.225586 C 1.350911 5.668945 1.741536 5.162761 2.197266 4.707031 C 2.652995 4.251303 3.157552 3.859051 3.710938 3.530273 C 4.264323 3.201498 4.861653 2.947592 5.50293 2.768555 C 6.144206 2.58952 6.809896 2.5 7.5 2.5 L 14.121094 2.5 L 12.685547 1.064453 C 12.561849 0.940756 12.5 0.794271 12.5 0.625 C 12.5 0.45573 12.561849 0.309246 12.685547 0.185547 C 12.809244 0.06185 12.955729 0 13.125 0 C 13.294271 0 13.440755 0.06185 13.564453 0.185547 L 16.064453 2.685547 C 16.18815 2.809246 16.25 2.95573 16.25 3.125 C 16.25 3.294271 16.18815 3.440756 16.064453 3.564453 L 13.564453 6.064453 C 13.440755 6.188151 13.294271 6.25 13.125 6.25 C 12.955729 6.25 12.809244 6.188151 12.685547 6.064453 C 12.561849 5.940756 12.5 5.794271 12.5 5.625 C 12.5 5.455729 12.561849 5.309245 12.685547 5.185547 L 14.121094 3.75 Z M 20 10 C 20 10.690104 19.91048 11.352539 19.731445 11.987305 C 19.552408 12.62207 19.300129 13.217773 18.974609 13.774414 C 18.649088 14.331055 18.258463 14.83724 17.802734 15.292969 C 17.347004 15.748698 16.842447 16.140951 16.289062 16.469727 C 15.735677 16.798502 15.138346 17.052408 14.49707 17.231445 C 13.855793 17.410482 13.190104 17.5 12.5 17.5 L 5.888672 17.5 L 7.314453 18.935547 C 7.43815 19.059244 7.5 19.205729 7.5 19.375 C 7.5 19.544271 7.43815 19.690756 7.314453 19.814453 C 7.190755 19.93815 7.044271 20 6.875 20 C 6.705729 20 6.559244 19.93815 6.435547 19.814453 L 3.935547 17.314453 C 3.811849 17.190756 3.75 17.044271 3.75 16.875 C 3.75 16.705729 3.811849 16.559244 3.935547 16.435547 L 6.435547 13.935547 C 6.559244 13.81185 6.705729 13.75 6.875 13.75 C 7.044271 13.75 7.190755 13.81185 7.314453 13.935547 C 7.43815 14.059245 7.5 14.205729 7.5 14.375 C 7.5 14.544271 7.43815 14.690756 7.314453 14.814453 L 5.888672 16.25 L 12.5 16.25 C 13.365885 16.25 14.178059 16.08724 14.936523 15.761719 C 15.694986 15.436198 16.357422 14.990234 16.923828 14.423828 C 17.490234 13.857422 17.936197 13.194987 18.261719 12.436523 C 18.587238 11.678061 18.75 10.865886 18.75 10 C 18.75 9.628906 18.723957 9.283854 18.671875 8.964844 C 18.619791 8.645834 18.541666 8.336589 18.4375 8.037109 C 18.333332 7.737631 18.204752 7.444662 18.051758 7.158203 C 17.898762 6.871746 17.721354 6.575521 17.519531 6.269531 C 17.473957 6.197917 17.441406 6.139323 17.421875 6.09375 C 17.402344 6.048178 17.392578 5.983074 17.392578 5.898438 C 17.392578 5.729168 17.452799 5.581056 17.573242 5.454102 C 17.693684 5.327149 17.841797 5.263673 18.017578 5.263672 C 18.128254 5.263673 18.221027 5.286459 18.295898 5.332031 C 18.370768 5.377604 18.44401 5.439453 18.515625 5.517578 C 18.75651 5.797527 18.969727 6.119793 19.155273 6.484375 C 19.34082 6.848959 19.495441 7.231445 19.619141 7.631836 C 19.742838 8.032227 19.837238 8.435873 19.902344 8.842773 C 19.967447 9.249675 20 9.635417 20 10 Z "),
                Public.Enum.Setting.Repeat.RepeatOne => RepeatBtn.Data = PathGeometry.Parse(
                    "F1 M 7.5 3.75 C 6.634114 3.75 5.82194 3.912762 5.063477 4.238281 C 4.305013 4.563803 3.642578 5.009766 3.076172 5.576172 C 2.509766 6.142578 2.063802 6.805014 1.738281 7.563477 C 1.41276 8.32194 1.25 9.134115 1.25 10 C 1.25 10.384115 1.277669 10.73405 1.333008 11.049805 C 1.388346 11.365561 1.468099 11.669922 1.572266 11.962891 C 1.676432 12.255859 1.806641 12.543945 1.962891 12.827148 C 2.119141 13.110352 2.294922 13.411459 2.490234 13.730469 C 2.529297 13.795573 2.560221 13.855795 2.583008 13.911133 C 2.605794 13.966472 2.617188 14.029948 2.617188 14.101562 C 2.617188 14.270834 2.553711 14.417318 2.426758 14.541016 C 2.299805 14.664714 2.151693 14.726562 1.982422 14.726562 C 1.871745 14.726562 1.778971 14.703776 1.704102 14.658203 C 1.629232 14.612631 1.55599 14.550781 1.484375 14.472656 C 1.25 14.199219 1.040039 13.880209 0.854492 13.515625 C 0.668945 13.151042 0.512695 12.766928 0.385742 12.363281 C 0.258789 11.959636 0.16276 11.552734 0.097656 11.142578 C 0.032552 10.732422 0 10.351562 0 10 C 0 8.990886 0.188802 8.032227 0.566406 7.124023 C 0.94401 6.215821 1.48763 5.406902 2.197266 4.697266 C 2.906901 3.987631 3.71582 3.444012 4.624023 3.066406 C 5.532227 2.688803 6.490885 2.5 7.5 2.5 L 14.121094 2.5 L 12.685547 1.064453 C 12.561849 0.940756 12.5 0.794271 12.5 0.625 C 12.5 0.45573 12.561849 0.309246 12.685547 0.185547 C 12.809244 0.06185 12.955729 0 13.125 0 C 13.294271 0 13.440755 0.06185 13.564453 0.185547 L 16.064453 2.685547 C 16.18815 2.809246 16.25 2.95573 16.25 3.125 C 16.25 3.294271 16.18815 3.440756 16.064453 3.564453 L 13.564453 6.064453 C 13.440755 6.188151 13.294271 6.25 13.125 6.25 C 12.955729 6.25 12.809244 6.188151 12.685547 6.064453 C 12.561849 5.940756 12.5 5.794271 12.5 5.625 C 12.5 5.455729 12.561849 5.309245 12.685547 5.185547 L 14.121094 3.75 Z M 17.392578 5.898438 C 17.392578 5.729168 17.452799 5.581056 17.573242 5.454102 C 17.693684 5.327149 17.841797 5.263673 18.017578 5.263672 C 18.128254 5.263673 18.221027 5.286459 18.295898 5.332031 C 18.370768 5.377604 18.44401 5.439453 18.515625 5.517578 C 18.75 5.784506 18.958332 6.097006 19.140625 6.455078 C 19.322916 6.813152 19.47591 7.189129 19.599609 7.583008 C 19.723307 7.976889 19.819336 8.374023 19.887695 8.774414 C 19.956055 9.174805 19.993488 9.550781 20 9.902344 C 19.791666 9.648438 19.570312 9.405925 19.335938 9.174805 C 19.101562 8.943686 18.85091 8.733725 18.583984 8.544922 C 18.479816 8.115235 18.334961 7.721355 18.149414 7.363281 C 17.963867 7.005209 17.753906 6.640625 17.519531 6.269531 C 17.473957 6.197917 17.441406 6.139323 17.421875 6.09375 C 17.402344 6.048178 17.392578 5.983074 17.392578 5.898438 Z M 20 14.375 C 20 15.14974 19.851887 15.878906 19.555664 16.5625 C 19.259439 17.246094 18.857422 17.841797 18.349609 18.349609 C 17.841797 18.857422 17.246094 19.259439 16.5625 19.555664 C 15.878906 19.851889 15.149739 20 14.375 20 C 13.59375 20 12.861328 19.853516 12.177734 19.560547 C 11.494141 19.267578 10.898438 18.867188 10.390625 18.359375 C 9.882812 17.851562 9.482422 17.255859 9.189453 16.572266 C 8.896484 15.888672 8.75 15.15625 8.75 14.375 C 8.75 13.600261 8.898111 12.871094 9.194336 12.1875 C 9.49056 11.503906 9.892578 10.908203 10.400391 10.400391 C 10.908203 9.892578 11.503906 9.490561 12.1875 9.194336 C 12.871093 8.898112 13.60026 8.75 14.375 8.75 C 14.889322 8.75 15.385741 8.816732 15.864258 8.950195 C 16.342773 9.083659 16.790363 9.272461 17.207031 9.516602 C 17.623697 9.760742 18.004557 10.055339 18.349609 10.400391 C 18.69466 10.745443 18.989258 11.126303 19.233398 11.542969 C 19.477539 11.959636 19.66634 12.407227 19.799805 12.885742 C 19.933268 13.364258 20 13.860678 20 14.375 Z M 15 11.875 C 14.999999 11.705729 14.93815 11.559245 14.814453 11.435547 C 14.690755 11.31185 14.544271 11.25 14.375 11.25 C 14.205729 11.25 14.059244 11.31185 13.935547 11.435547 L 12.685547 12.685547 C 12.561849 12.809245 12.5 12.955729 12.5 13.125 C 12.5 13.294271 12.561849 13.440756 12.685547 13.564453 C 12.809244 13.688151 12.955729 13.75 13.125 13.75 C 13.268229 13.75 13.385416 13.712565 13.476562 13.637695 C 13.567708 13.562826 13.658854 13.479818 13.75 13.388672 L 13.75 16.875 C 13.75 17.044271 13.811849 17.190756 13.935547 17.314453 C 14.059244 17.43815 14.205729 17.5 14.375 17.5 C 14.544271 17.5 14.690755 17.43815 14.814453 17.314453 C 14.93815 17.190756 14.999999 17.044271 15 16.875 Z M 7.431641 16.25 C 7.548828 16.686197 7.705078 17.102865 7.900391 17.5 L 5.888672 17.5 L 7.314453 18.935547 C 7.43815 19.059244 7.5 19.205729 7.5 19.375 C 7.5 19.544271 7.43815 19.690756 7.314453 19.814453 C 7.190755 19.93815 7.044271 20 6.875 20 C 6.705729 20 6.559244 19.93815 6.435547 19.814453 L 3.935547 17.314453 C 3.811849 17.190756 3.75 17.044271 3.75 16.875 C 3.75 16.705729 3.811849 16.559244 3.935547 16.435547 L 6.435547 13.935547 C 6.559244 13.81185 6.705729 13.75 6.875 13.75 C 7.005208 13.75 7.115885 13.782553 7.207031 13.847656 C 7.200521 13.938803 7.195638 14.02832 7.192383 14.116211 C 7.189127 14.204102 7.1875 14.29362 7.1875 14.384766 C 7.1875 14.475912 7.189127 14.56543 7.192383 14.65332 C 7.195638 14.741211 7.200521 14.830729 7.207031 14.921875 L 5.888672 16.25 Z "),
                Public.Enum.Setting.Repeat.RepeatOff => RepeatBtn.Data = PathGeometry.Parse(
                    "F1 M 20 19.375 C 20 19.544271 19.93815 19.690756 19.814453 19.814453 C 19.690754 19.93815 19.54427 20 19.375 20 C 19.205729 20 19.059244 19.93815 18.935547 19.814453 L 0.185547 1.064453 C 0.061849 0.940756 0 0.794271 0 0.625 C 0 0.45573 0.061849 0.309246 0.185547 0.185547 C 0.309245 0.06185 0.455729 0 0.625 0 C 0.794271 0 0.940755 0.06185 1.064453 0.185547 L 19.814453 18.935547 C 19.93815 19.059244 20 19.205729 20 19.375 Z M 7.5 3.75 C 7.330729 3.75 7.161458 3.756512 6.992188 3.769531 C 6.822916 3.782553 6.653646 3.802084 6.484375 3.828125 L 5.439453 2.792969 C 6.103516 2.597656 6.790364 2.5 7.5 2.5 L 14.121094 2.5 L 12.685547 1.064453 C 12.561849 0.940756 12.5 0.794271 12.5 0.625 C 12.5 0.45573 12.561849 0.309246 12.685547 0.185547 C 12.809244 0.06185 12.955729 0 13.125 0 C 13.294271 0 13.440755 0.06185 13.564453 0.185547 L 16.064453 2.685547 C 16.18815 2.809246 16.25 2.95573 16.25 3.125 C 16.25 3.294271 16.18815 3.440756 16.064453 3.564453 L 13.564453 6.064453 C 13.440755 6.188151 13.294271 6.25 13.125 6.25 C 12.955729 6.25 12.809244 6.188151 12.685547 6.064453 C 12.561849 5.940756 12.5 5.794271 12.5 5.625 C 12.5 5.455729 12.561849 5.309245 12.685547 5.185547 L 14.121094 3.75 Z M 3.066406 3.955078 L 3.964844 4.84375 C 3.535156 5.130209 3.152669 5.463867 2.817383 5.844727 C 2.482096 6.225586 2.198893 6.63737 1.967773 7.080078 C 1.736654 7.522787 1.559245 7.991537 1.435547 8.486328 C 1.311849 8.98112 1.25 9.485678 1.25 10 C 1.25 10.709636 1.355794 11.363933 1.567383 11.962891 C 1.778971 12.56185 2.083333 13.151042 2.480469 13.730469 C 2.526042 13.795573 2.558594 13.852539 2.578125 13.901367 C 2.597656 13.950195 2.607422 14.016928 2.607422 14.101562 C 2.607422 14.270834 2.545573 14.418945 2.421875 14.545898 C 2.298177 14.672852 2.151693 14.736328 1.982422 14.736328 C 1.871745 14.736328 1.778971 14.713542 1.704102 14.667969 C 1.629232 14.622396 1.55599 14.560547 1.484375 14.482422 C 1.39974 14.391276 1.323242 14.287109 1.254883 14.169922 C 1.186523 14.052734 1.119792 13.942058 1.054688 13.837891 C 0.703125 13.251953 0.439453 12.635092 0.263672 11.987305 C 0.087891 11.339519 0 10.677084 0 10 C 0 8.769531 0.268555 7.638347 0.805664 6.606445 C 1.342773 5.574545 2.096354 4.690756 3.066406 3.955078 Z M 16.992188 14.345703 C 17.552082 13.759766 17.985025 13.092448 18.291016 12.34375 C 18.597004 11.595053 18.75 10.813803 18.75 10 C 18.75 9.290365 18.644205 8.636068 18.432617 8.037109 C 18.221027 7.438152 17.916666 6.848959 17.519531 6.269531 C 17.473957 6.204428 17.441406 6.147462 17.421875 6.098633 C 17.402344 6.049806 17.392578 5.983074 17.392578 5.898438 C 17.392578 5.729168 17.454426 5.581056 17.578125 5.454102 C 17.701822 5.327149 17.848307 5.263673 18.017578 5.263672 C 18.128254 5.263673 18.221027 5.286459 18.295898 5.332031 C 18.370768 5.377604 18.44401 5.439453 18.515625 5.517578 C 18.587238 5.602215 18.655598 5.694988 18.720703 5.795898 C 18.785807 5.896811 18.847656 5.996094 18.90625 6.09375 C 19.264322 6.686198 19.536133 7.312826 19.72168 7.973633 C 19.907227 8.63444 20 9.309896 20 10 C 20 10.976562 19.817707 11.91569 19.453125 12.817383 C 19.088541 13.719076 18.564453 14.521484 17.880859 15.224609 Z M 5.888672 16.25 L 12.5 16.25 C 13.320312 16.25 14.117838 16.090496 14.892578 15.771484 L 15.830078 16.71875 C 15.309244 16.972656 14.770508 17.166342 14.213867 17.299805 C 13.657227 17.433268 13.085938 17.5 12.5 17.5 L 5.888672 17.5 L 7.314453 18.935547 C 7.43815 19.059244 7.5 19.205729 7.5 19.375 C 7.5 19.544271 7.43815 19.690756 7.314453 19.814453 C 7.190755 19.93815 7.044271 20 6.875 20 C 6.705729 20 6.559244 19.93815 6.435547 19.814453 L 3.935547 17.314453 C 3.811849 17.190756 3.75 17.044271 3.75 16.875 C 3.75 16.705729 3.811849 16.559244 3.935547 16.435547 L 6.435547 13.935547 C 6.559244 13.81185 6.705729 13.75 6.875 13.75 C 7.044271 13.75 7.190755 13.81185 7.314453 13.935547 C 7.43815 14.059245 7.5 14.205729 7.5 14.375 C 7.5 14.544271 7.43815 14.690756 7.314453 14.814453 Z "),
                _ => RepeatBtn.Data
            };
            _ = Animator.PageLoading.LevelTwoPage(LeftControl.Content as UserControl);
            _ = Animator.PageLoading.LevelTwoPage(RightControl.Content as UserControl);
        };
        DownloadBtn.PointerPressed += async (_, _) =>
        {
            var song = Data.UiProperty.SelectedRecordSong;
            if (song == null) return;
            var path = (await TopLevel.GetTopLevel(this).StorageProvider.SaveFilePickerAsync(
                new FilePickerSaveOptions
                {
                    Title = MainLang.SaveAudioFile,
                    SuggestedFileName = $"{song.SongName}.mp3",
                    FileTypeChoices =
                    [
                        new FilePickerFileType("Audio File") { Patterns = ["*.mp3"] }
                    ]
                })).Path.LocalPath;
            if (string.IsNullOrWhiteSpace(path)) return;
            if (song.Type == SongType.Local)
            {
                try
                {
                    Notice($"{MainLang.BeginDownload}: {song.SongName}.mp3");
                    File.Copy(song.Path, path);
                    Notice($"{MainLang.DownloadFinish}: {song.SongName}.mp3", NotificationType.Success);
                }
                catch
                {
                    Notice($"{MainLang.DownloadFail}: {song.SongName}.mp3", NotificationType.Error);
                }
            }
            else
            {
                var task = new TaskEntry($"{MainLang.Download} - {song.SongName}.mp3",
                    [new SubTask($"{MainLang.Download} - {song.SongName}.mp3")]);
                Notice($"{MainLang.BeginDownload}: {song.SongName}.mp3");
                try
                {
                    var ava = await NeteaseMusic.GetMusicAvailabilityById(song.SongId);
                    if (!ava)
                    {
                        Notice(MainLang.MusicNotAvailable, NotificationType.Error);
                        task.FinishWithError();
                        return;
                    }

                    var url = await NeteaseMusic.GetSongUrlByIdAndLevel(song.SongId);
                    using var client1 = new HttpClient();
                    using var response1 =
                        await client1.GetAsync(url.url, HttpCompletionOption.ResponseHeadersRead);
                    response1.EnsureSuccessStatusCode();
                    var totalBytes = response1.Content.Headers.ContentLength.GetValueOrDefault();
                    await using Stream contentStream = await response1.Content.ReadAsStreamAsync(),
                        fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None,
                            8192, true);
                    long totalBytesRead = 0;
                    var buffer = new byte[8192];
                    int bytesRead;
                    while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
                    {
                        await fileStream.WriteAsync(buffer, 0, bytesRead);
                        totalBytesRead += bytesRead;
                        var progressPercentage = (double)totalBytesRead / totalBytes * 100;
                        task.UpdateValue(progressPercentage);
                    }

                    Notice($"{MainLang.DownloadFinish}: {song.SongName}.mp3", NotificationType.Success);
                    task.AdvanceSubTask();
                    task.FinishWithSuccess();
                    task.Destroy();
                }
                catch
                {
                    Notice($"{MainLang.DownloadFail}: {song.SongName}.mp3", NotificationType.Error);
                    task.FinishWithError();
                }
            }
        };
    }

    public void PlayUi()
    {
        _isPlaying = true;
        PauseIcon.IsVisible = false;
        PlayingIcon.IsVisible = true;
        AudioPlayer.Instance.Resume();
    }

    public void PauseUi()
    {
        _isPlaying = false;
        PauseIcon.IsVisible = true;
        PlayingIcon.IsVisible = false;
        AudioPlayer.Instance.Pause();
    }

    public void ToggleLoadingUi(bool isLoading)
    {
        if (isLoading)
        {
            LoadingBar.IsVisible = true;
            PlayerSlider.IsVisible = false;
            ControlPlayerSlider.IsVisible = false;
        }
        else
        {
            LoadingBar.IsVisible = false;
            PlayerSlider.IsVisible = true;
            ControlPlayerSlider.IsVisible = true;
        }
    }

    public void SearchFormCall(string query)
    {
        if (_isLyric)
        {
            LeftControl.Content = Search;
            _isLyric = false;
        }
        Search.SearchBox.Text = query;
        _ = Search.SearchAction();
    }
}